package top.chmin.project.cloud.common.code.template.auto;

// field import auto
// #bind.field.fixLength#

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import top.chmin.project.cloud.common.entity.Page;
import top.chmin.project.cloud.common.tool.DatetimeTool;
import top.chmin.project.cloud.common.tool.LocalTool;

import java.time.LocalDateTime;
import java.util.List;

/**
 * chmin
 * 2023-12-27 19:17:31
 * cloud-builder
 */
@Slf4j
@Component
public class CodeTemplatePlugin {

    public void checkSave(CodeTemplateSave.Param param) {
        log.debug("checkSave-param: {}", JSONUtil.toJsonStr(param));
        if (param == null) {
            throw new RuntimeException("保存对象不能为空");
        }
    }

    public void beforeSave(CodeTemplate codeTemplate) {
        log.debug("beforeSave-codeTemplate: {}", JSONUtil.toJsonStr(codeTemplate));
        codeTemplate.setCreateBy(LocalTool.getUserId());
        codeTemplate.setUpdateBy(LocalTool.getUserId());
        codeTemplate.setCreateTime(LocalDateTime.now());
        codeTemplate.setUpdateTime(LocalDateTime.now());

        if (StringUtils.isEmpty(codeTemplate.getPrefix())) {
            codeTemplate.setPrefix("");
        }
        if (StringUtils.isEmpty(codeTemplate.getCodeDateTimeFormat())) {
            codeTemplate.setCodeDateTimeFormat("");
        }
        if (codeTemplate.getInitValue() == null) {
            codeTemplate.setInitValue(1);
        }
        if (codeTemplate.getMinStep() == null) {
            codeTemplate.setMinStep(1);
        }
        if (codeTemplate.getMaxStep() == null) {
            codeTemplate.setMaxStep(1);
        }
        if (codeTemplate.getMinStep() < 1) {
            codeTemplate.setMinStep(1);
        }
        if (codeTemplate.getFixLength() == null) {
            codeTemplate.setFixLength(1);
        }
    }

    public void afterSave(CodeTemplateSave.Param param, CodeTemplate codeTemplate, int result) {
        log.debug("afterSave-param: {}", JSONUtil.toJsonStr(param));
        log.debug("afterSave-codeTemplate: {}", JSONUtil.toJsonStr(codeTemplate));
        log.debug("afterSave-result: {}", JSONUtil.toJsonStr(result));

    }

    public void checkUpdateById(CodeTemplateUpdateById.Param param) {
        log.debug("checkUpdateById-param: {}", JSONUtil.toJsonStr(param));
        if (param == null) {
            throw new RuntimeException("更新对象不能为空");
        }
        if (param.getId() == null) {
            throw new RuntimeException("更新对象id不能为空");
        }
    }

    public void beforeUpdateById(CodeTemplate codeTemplate) {
        log.debug("beforeUpdateById-codeTemplate: {}", JSONUtil.toJsonStr(codeTemplate));
        codeTemplate.setUpdateBy(LocalTool.getUserId());
        codeTemplate.setUpdateTime(LocalDateTime.now());
    }

    public void afterUpdateById(CodeTemplateUpdateById.Param param, CodeTemplate codeTemplate, int result) {
        log.debug("afterUpdateById-param: {}", JSONUtil.toJsonStr(param));
        log.debug("afterUpdateById-codeTemplate: {}", JSONUtil.toJsonStr(codeTemplate));
        log.debug("afterUpdateById-result: {}", JSONUtil.toJsonStr(result));

    }

    public void checkDeleteById(CodeTemplateDeleteById.Param param) {
        log.debug("checkDeleteById-param: {}", JSONUtil.toJsonStr(param));
        if (param == null) {
            throw new RuntimeException("删除对象不能为空");
        }
        if (param.getId() == null) {
            throw new RuntimeException("删除对象id不能为空");
        }
    }

    public void afterDeleteById(CodeTemplateDeleteById.Param param, int result) {
        log.debug("afterDeleteById-param: {}", JSONUtil.toJsonStr(param));
        log.debug("afterDeleteById-result: {}", JSONUtil.toJsonStr(result));

    }

    public void checkSelectById(CodeTemplateSelectById.Param param) {
        log.debug("checkSelectById-param: {}", JSONUtil.toJsonStr(param));
        if (param == null) {
            throw new RuntimeException("查询对象不能为空");
        }
        if (param.getId() == null) {
            throw new RuntimeException("查询对象id不能为空");
        }
    }

    public void beforeSelectById(CodeTemplateSelectById.Param param, CodeTemplateFPC fpc) {
        log.debug("beforeSelectById-param: {}", JSONUtil.toJsonStr(param));

    }

    public void afterSelectById(CodeTemplateSelectById.Param param, CodeTemplateSelectById.Result result) {
        log.debug("afterSelectById-param: {}", JSONUtil.toJsonStr(param));
        log.debug("afterSelectById-result: {}", JSONUtil.toJsonStr(result));

    }

    public void checkSelectPage(CodeTemplateSelectPage.Param param) {
        log.debug("checkSelectPage-param: {}", JSONUtil.toJsonStr(param));
        if (param == null) {
            throw new RuntimeException("分页对象不能为空");
        }
    }

    public void beforeSelectPage(CodeTemplateSelectPage.Param param, CodeTemplateFPC fpc) {
        log.debug("beforeSelectPage-param: {}", JSONUtil.toJsonStr(param));
// field beforeSelectPage auto
// #bind.field.createBy#
        fpc.createBy().eq(param.getCreateBy());
// $bind.field.createBy$

// #bind.field.updateBy#
        fpc.updateBy().eq(param.getUpdateBy());
// $bind.field.updateBy$

// #bind.field.createTime#
        fpc.createTime().ge(param.getCreateTime() == null || param.getCreateTime().length == 0 ? null : param.getCreateTime()[0]);
        fpc.createTime().lt(param.getCreateTime() == null || param.getCreateTime().length < 2 ? null : DatetimeTool.nextDay(param.getCreateTime()[1]));
// $bind.field.createTime$

// #bind.field.updateTime#
        fpc.updateTime().ge(param.getUpdateTime() == null || param.getUpdateTime().length == 0 ? null : param.getUpdateTime()[0]);
        fpc.updateTime().lt(param.getUpdateTime() == null || param.getUpdateTime().length < 2 ? null : DatetimeTool.nextDay(param.getUpdateTime()[1]));
// $bind.field.updateTime$

// #bind.field.codeName#
        fpc.codeName().eq(param.getCodeName());
// $bind.field.codeName$

// #bind.field.prefix#
        fpc.prefix().eq(param.getPrefix());
// $bind.field.prefix$

// #bind.field.codeDateTimeFormat#
        fpc.codeDateTimeFormat().eq(param.getCodeDateTimeFormat());
// $bind.field.codeDateTimeFormat$

// #bind.field.initValue#
        fpc.initValue().eq(param.getInitValue());
// $bind.field.initValue$

// #bind.field.minStep#
        fpc.minStep().eq(param.getMinStep());
// $bind.field.minStep$

// #bind.field.maxStep#
        fpc.maxStep().eq(param.getMaxStep());
// $bind.field.maxStep$

// #bind.field.fixLength#
        fpc.fixLength().eq(param.getFixLength());
// $bind.field.fixLength$

    }

    public void afterSelectPage(CodeTemplateSelectPage.Param param, Page<CodeTemplateSelectPage.Result> result) {
        log.debug("afterSelectPage-param: {}", JSONUtil.toJsonStr(param));
        log.debug("afterSelectPage-result: {}", JSONUtil.toJsonStr(result));

    }

    // entity select auto
    // entity select auto
    public void checkSelectCodeName(CodeTemplateCodeNameSelect.Param param) {
        log.debug("checkSelectCodeName-param: {}", JSONUtil.toJsonStr(param));
    }

    public void beforeSelectCodeName(CodeTemplateCodeNameSelect.Param param, CodeTemplateFPC fpc) {
        log.debug("beforeSelectCodeName-param: {}", JSONUtil.toJsonStr(param));
        log.debug("beforeSelectCodeName-fpc: {}", JSONUtil.toJsonStr(fpc));

    }

    public void afterSelectCodeName(CodeTemplateCodeNameSelect.Param param, List<CodeTemplateCodeNameSelect.Result> result) {
        log.debug("afterSelectCodeName-param: {}", JSONUtil.toJsonStr(param));
        log.debug("afterSelectCodeName-result: {}", JSONUtil.toJsonStr(result));

    }


// entity search auto
}
